package com.gtone.usbtool;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.gtone.usbtool.bean.EncryptCode;
import com.gtone.usbtool.datatool.DataBaseTool;
import com.gtone.usbtool.encrypt.AESUtils;
import com.gtone.usbtool.popview.USBDialog;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Button cancelBtn, popBtn;
    private Button submitBtn;
    private TextView infoTv, showCodeTv, noteTv;
    private EditText yanzEdt;

    private DataBaseTool dataBaseTool;
    private EncryptCode encryptCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestPermission();
        init();
        setInfo();
    }

    private void init() {
        cancelBtn = findViewById(R.id.decrypt_btn);
        submitBtn = findViewById(R.id.permission_btn);
        popBtn = findViewById(R.id.pop_btn);
        infoTv = findViewById(R.id.info_tv);
        showCodeTv = findViewById(R.id.show_code_tv);
        noteTv = findViewById(R.id.note_tv);
        yanzEdt = findViewById(R.id.yanz_edt);

        submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String cusValue = yanzEdt.getText().toString().trim();
                String localValue = AESUtils.decrypt(encryptCode.getValue(), AESUtils.KEY);
                if (cusValue.equalsIgnoreCase(localValue)) {
                    Toast.makeText(MainActivity.this, "匹配成功", Toast.LENGTH_SHORT).show();
                    noteTv.setText("匹配成功");
                } else {
                    Toast.makeText(MainActivity.this, "匹配失败", Toast.LENGTH_SHORT).show();
                    noteTv.setText("匹配失败");
                }
            }
        });

        popBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                USBDialog usbDialog = new USBDialog(MainActivity.this);
                usbDialog.show();

            }
        });

        dataBaseTool = new DataBaseTool(this);
    }

    @SuppressLint("SetTextI18n")
    private void setInfo() {
        encryptCode = dataBaseTool.randomData();
        String identifyCode = AESUtils.decrypt(encryptCode.getKey(), AESUtils.KEY);
        if (identifyCode == null) {
            showCodeTv.setText("未发现密码库，请联系管理员导入文件后重试");
        } else {
            showCodeTv.setText("识别码:" + identifyCode);
        }

    }

    /**
     * 申请存储权限(Manifest内同样要配置参数)
     */
    private void requestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (!Environment.isExternalStorageManager()) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                startActivity(intent);
            }
        } else {
            AndPermission.with(this)
                    .runtime()
                    .permission(Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE)
                    .onDenied(new Action<List<String>>() {
                        @Override
                        public void onAction(List<String> data) {
                            if (data != null) {
                                for (String datum : data) {
                                    Log.d("Denied", datum);
                                }
                            }
                        }
                    }).onGranted(new Action<List<String>>() {
                        @Override
                        public void onAction(List<String> data) {
                            if (data != null) {
                                for (String datum : data) {
                                    Log.d("Granted", datum);
                                }
                            }
                        }
                    }).start();
        }
    }
}
